<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--客户卡券数据-->
      <el-col :span="24" :xs="24">
      
        <!-- 搜索 -->
        <template>
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
            <el-form-item label="卡券名称" prop="couponTitle">
              <el-input
                v-model="queryParams.couponTitle"
                placeholder="请输入名称"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="客户ID" prop="customerId">
              <el-input
                v-model="queryParams.customerId"
                placeholder="请输入客户ID"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="卡券ID" prop="couponId">
              <el-input
                v-model="queryParams.couponId"
                placeholder="请输入卡券ID"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="使用状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="客户卡券状态"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in dict.type.app_customer_coupon_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="激活状态" prop="active">
              <el-select
                v-model="queryParams.active"
                placeholder="激活状态"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="店铺ID" prop="shopId" v-if="$store.getters && $store.getters.admin">
              <el-input
                v-model="queryParams.shopId"
                placeholder="请输入店铺ID"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="创建时间">
              <el-date-picker
                v-model="dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </template>

        <!-- 按钮操作栏 -->
        <template>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['customerCoupon:coupon:remove']"
              >删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
        </template>

        <!-- 表格 -->
        <template>
          <el-table v-loading="loading" :data="couponList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column type="index" label="序号" width="50" align="center" />
            <!-- <el-table-column label="编号ID" align="center" key="id" prop="id" /> -->
            <el-table-column label="名称" align="center" key="couponTitle" prop="couponTitle" minWidth="200px" :show-overflow-tooltip="true" />
            <el-table-column label="客户" align="center" key="customerId" prop="customerId" :show-overflow-tooltip="true" width="120" >
              <template slot-scope="scope">
                <div class="flex-align-center" v-if="scope.row.customerUser">
                  <el-avatar shape="circle" size="medium" :src="scope.row.customerUser.avatar" alt="404"></el-avatar>
                  <div class="mgl5 font-14">{{scope.row.customerUser.nickName}}</div>
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
            <el-table-column label="卡券ID" align="center" key="couponId" prop="couponId" />
            <el-table-column label="商品ID" align="center" key="goodsId" prop="goodsId" />
            <el-table-column label="价格" align="center" key="couponPrice" prop="couponPrice" :show-overflow-tooltip="true" />
            <el-table-column label="金额门槛" align="center" key="couponPriceThreshold" prop="couponPriceThreshold" :show-overflow-tooltip="true" />
            <el-table-column label="使用状态" align="center" key="status" prop="status">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.app_customer_coupon_status" :value="scope.row.status"/>
              </template>
            </el-table-column>
            <el-table-column label="激活状态" align="center" key="active">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.active"
                  active-value="0"
                  inactive-value="1"
                  @change="handleStatusChange(scope.row)"
                ></el-switch>
              </template>
            </el-table-column>
            <el-table-column label="店铺名称" prop="shopInfo.shopName" v-if="$store.getters && $store.getters.admin" />
            <el-table-column label="创建时间" align="center" prop="createTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="160">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="使用时间" align="center" prop="useTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="160">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.useTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              align="center"
              width="160"
              fixed="right"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['customerCoupon:coupon:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </template>

      </el-col>
    </el-row>
  </div>
</template>

<script>
import { customerCouponListAll, delCustomerCoupon, customerCouponChangeStatus } from "@/api/customer/coupon";

export default {
  name: "coupon",
  dicts: ['sys_normal_disable', 'app_customer_coupon_status'],
  components: { },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户卡券表格数据
      couponList: null,
      // 弹出层标题
      title: "",
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponTitle: undefined,
        customerId: undefined,
        couponId: undefined,
        status: undefined,
        active: undefined,
        shopId: undefined,
      },
    };
  },
  computed: {
  },
  watch: {
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户卡券列表 */
    getList() {
      this.loading = true;
      customerCouponListAll(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.couponList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 客户卡券状态修改
    handleStatusChange(row) {
      let text = row.active === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.couponTitle + '"客户卡券吗？').then(function() {
        return customerCouponChangeStatus(row.id, row.active);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.active = row.active === "0" ? "1" : "0";
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        couponTitle: undefined,
        customerId: undefined,
        couponId: undefined,
        status: undefined,
        active: undefined,
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户卡券编号为"' + ids + '"的数据项？').then(function() {
        return delCustomerCoupon(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>